/*
 * mysql_lib.c
 *
 *  Created on: 2011-6-26
 *      Author: vyouzhi
 */

#include <mysql.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

#include "mysql_lib.h"
#include "sdb_struct.h"
#include "conf_lib.h"
#include "mem_lib.h"


MYSQL *connect_sql(void){
	MYSQL  *conn;
	char *mysql_host, *mysql_user, *mysql_pwd, *mysql_db, *mysql_char;
	int time_out = 3;

	mysql_host = conf_get("mysql_host");
	mysql_user = conf_get("mysql_user");
	mysql_pwd = conf_get("mysql_pwd");
	mysql_db = conf_get("mysql_db");
	mysql_char = conf_get("mysql_char");

	conn = mysql_init(NULL);
	if(conn == NULL){
	    	printf("error-- mysql init\n");
	    	return NULL;
	}

	mysql_options(conn, MYSQL_SET_CHARSET_NAME, "utf8");
	mysql_options(conn, MYSQL_OPT_CONNECT_TIMEOUT, &time_out);

	if (!mysql_real_connect(conn, mysql_host, mysql_user, mysql_pwd, mysql_db, 0, NULL, 0)) {		
		printf( "error mysql connect %s\n", mysql_error(conn));
		close_mysql(conn);
		return NULL;
	}
	/*
	if (mysql_set_character_set(conn, mysql_char)){
		printf("error New client character set: %s\n", mysql_character_set_name(conn));
		free(conn);
	}*/
	if(-1 == query_mysql("set names 'utf8'", conn)){
		printf("error character set\n");
		return NULL;
	}

	return conn;
}

int query_mysql(const char *sub, MYSQL *q_conn){
	int len;
	len = strlen(sub);
	if (mysql_real_query(q_conn, sub, len)) {
		printf("---error: %s, sub: %s\n", mysql_error(q_conn), sub);
		close_mysql(q_conn);
		return (-1);
	}
	return (0);
}

void commit_mysql(MYSQL *c_conn){
	mysql_commit(c_conn);
}

long set_mysql(const char *sub, MYSQL *s_conn){
	my_ulonglong lastid;
	
	if(-1 == query_mysql(sub, s_conn)) return (-1);
	lastid = mysql_insert_id(s_conn);	
	commit_mysql(s_conn);
	close_mysql(s_conn);
	return (long)lastid;
}

char *escape_mysql(const char *sub, MYSQL *e_conn){
	char *sqlescape;
	int len = 0;

	len = strlen(sub);
	sqlescape = (char *)calloc(len+20, sizeof(char));
	mysql_real_escape_string(e_conn, sqlescape, sub, len);
	return sqlescape;
}

int list_table(const char *ctable, MYSQL *l_conn){

	MYSQL_RES *res ;
	MYSQL_ROW row;

	res = mysql_list_tables(l_conn, ctable);
	row = mysql_fetch_row(res);

	mysql_field_seek(res, 0);	
	mysql_free_result(res);

	if(row == NULL ){
      		/* doesn't exists! */
		
		return -1;
	}
	else if(row){
	      /*The table exists 
		while(row){
                  printf("%s", row[0]);
	        }  */
		return 0; 
	}
	else{
	      /* error occurred!*/
		return -1;
	}
}

char *simple_mysql(const char *sub, MYSQL *si_conn){
	char *val = NULL;
	MYSQL_RES *res;
	MYSQL_ROW row;

	if(-1 == mysql_query(si_conn, sub)) return NULL;
	res = mysql_use_result(si_conn);

	if (res == NULL){
		close_mysql(si_conn);
		return NULL;
	}
	if((row = mysql_fetch_row(res)) != NULL){
		val = row[0];
	}
	return val;
}

Sdb *get_mysql(const char *sub, int type, MYSQL *g_conn){
	int i , m;
	unsigned int num_fields;
	MYSQL_ROW row;
	MYSQL_RES *res;
	MYSQL_FIELD *field;

	Sdb *sdb;
	SdbRow *sdbrow;
	FieldName *h, *n, *t, *u, *r;

	/* send SQL query */
	if( -1 == query_mysql(sub, g_conn)) return NULL; 
	/*mysql_query(conn, sub);*/
	res = mysql_use_result(g_conn);
	if (res == NULL){
		close_mysql(g_conn);
		return NULL;
	}
	num_fields = mysql_num_fields(res);

	
	h = (struct FieldName *)calloc(1, sizeof(struct FieldName));
	n = h;
	for(m=0; m<num_fields; m++){
        	t = (struct FieldName *)calloc(1, sizeof(struct FieldName));
		if (type == 0){
			field = mysql_fetch_field(res);
	        	t->fn = strdup(field->name);
		}else{
			t->fn = strdup("_0");
		}
        	n->next = t;
	        n = n->next;
   	}

	mysql_field_seek(res,0);
	sdb = Sdb_init();
	while ((row = mysql_fetch_row(res)) != NULL){
		sdbrow = SdbRow_init();
		u = h;
		for(i=0; i<num_fields; i++){
			u=u->next;
			SdbRow_addColumn(sdbrow, u->fn, row[i]);
		}
		Sdb_appendRow(sdb, sdbrow);
	}
	while(h){
		r = h->next; 
		SQLFree(h->fn);
		SQLFree(h);
		h = NULL;
		h = r;
	}
	mysql_field_seek(res, 0);	
	mysql_free_result(res);

	close_mysql(g_conn);	
	return sdb;
}

void close_mysql(MYSQL *c_conn){
	/* close connection */
	mysql_close(c_conn);
	mysql_library_end();
}
